package com.woniuxy.controller;

import com.woniuxy.entity.OrderDetail;
import com.woniuxy.entity.OrderEntity;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;

@RestController
@RequestMapping("/productionPlan")
@Api(tags = "生产订单接口")
public class OrderController {
    @Autowired
    OrderService orderService;


    @GetMapping("/order/{currentPage}/{pageSize}")
    @ApiOperation(value = "获取订单分页数据")
    public ResponseEntity findAllByPage(@ApiParam(name = "currentPage", value="当前页码") @PathVariable int currentPage,
                                        @ApiParam(name = "pageSize", value="页面大小")@PathVariable int pageSize){

        return orderService.findAllByPage(currentPage,pageSize);
    }


    @GetMapping("/order/")
    @ApiOperation(value = "获取所有生产订单信息")
    public ResponseEntity findAllOrder(){

        return orderService.findAllOrder();
    }

    @GetMapping("/order/{order_num}")
    @ApiOperation(value = "根据订单编码获取订单信息")
    public ResponseEntity findOneOrderByNum(@ApiParam(name = "order_num" , value = "订单编号") @PathVariable String order_num){
        return orderService.findAllOrderByNum(order_num);
    }

    @PutMapping("/order")
    @ApiOperation("更新订单数据")
    public ResponseEntity updateOrder(@ApiParam(name = "orderEntity", value = "更新的订单" ) @RequestBody OrderEntity orderEntity){
        return orderService.updateOrder(orderEntity);
    }

    @PostMapping("/order")
    @ApiOperation("添加订单信息")
    public ResponseEntity addOneOrder(@ApiParam(name = "orderEntity", value = "新增的订单" ) @RequestBody OrderEntity orderEntity){

        return orderService.addOneOrder(orderEntity);
    }

    @DeleteMapping("/order/{order_num}")
    @ApiOperation(value = "删除订单信息")
    public ResponseEntity deleteOne(@ApiParam(name = "order_num", value="删除订单的信息") @PathVariable String order_num){

        return orderService.deleteOrder(order_num);
    }

    @PutMapping("/positionOrders/{order_num}")
    @ApiOperation("下发订单")
    public ResponseEntity positionOrder(@ApiParam(name = "order_num", value = "下发订单，修改订单状态") @PathVariable String order_num){
        return orderService.positionOrder(order_num);
    }


    @PutMapping("/handleExitOrders/{order_num}")
    @ApiOperation("撤回订单")
    public ResponseEntity handleExit(@ApiParam(name = "order_num", value = "撤回订单，修改订单状态") @PathVariable String order_num){
        return orderService.handleExit(order_num);
    }


    @PutMapping("/terminationOrders/{order_num}")
    @ApiOperation("终止订单")
    public ResponseEntity termination(@ApiParam(name = "order_num", value = "终止订单，修改订单进度") @PathVariable String order_num){
        return orderService.termination(order_num);
    }


    @GetMapping("/detailOrders/{order_num}")
    @ApiOperation("订单详情")
    public ResponseEntity detailOrder(@ApiParam(name = "order_num", value = "根据订单号查看订单详情") @PathVariable String order_num){
        return orderService.detailOrder(order_num);
    }

    @GetMapping("/materialCodeData")
    @ApiOperation("产品编码")
    public ResponseEntity materialCodeData(){
        return orderService.materialCodeData();
    }

    @GetMapping("/productionNameAndType")
    @ApiOperation("获取产品名称和类型")
    public ResponseEntity productionNameAndType(@ApiParam(name = "material_code", value = "根据产品编码查找对应的产品名称和型号") @PathVariable String material_code){
        return orderService.productionNameAndType(material_code);
    }

    @GetMapping("/merchants")
    @ApiOperation("获取客商姓名")
    public ResponseEntity merchants(){
        return orderService.findMerchantsName();
    }

    @GetMapping(value = "/merchants/{name}")
    @ApiOperation("获取客商优先级")
    public ResponseEntity getmerchants_grade(@ApiParam(name = "name",value = "根据客商姓名查找客商优先级") @PathVariable String name){
        String newName = null;
        try {
            newName = URLDecoder.decode(name,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return orderService.getmerchants_grade(newName);
    }
}
